package com.jtny.nytb.equipment.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.WebResponse;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.constant.ResultConstant;
import com.jtny.nytb.equipment.service.EquipmentModelService;
import com.jtny.nytb.excel.utils.EasyExcelUtils;
import com.jtny.nytb.equipment.utils.ExcelSheetUtil;
import com.jtny.nytb.equipment.vo.excel.ExportExcelModelVO;
import com.jtny.nytb.equipment.vo.model.EquipmentModelPageQueryParamVO;
import com.jtny.nytb.equipment.vo.model.EquipmentModelParamVO;
import com.jtny.nytb.equipment.vo.model.EquipmentModelQueryPageParamVO;
import com.jtny.nytb.equipment.vo.model.EquipmentModelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;


@RestController
@Api(value = "设备模型管理", tags = "设备模型管理")
@ApiSort(204003)
@RequestMapping("/equipment-model")
@AllArgsConstructor
public class EquipmentModelController {

    private EquipmentModelService equipmentModelService;
    /**
     * 设备模型参数分页查询操作
     *
     * @param equipmentModelQueryPageParamVO
     * @return
     */
    @ApiOperation("设备模型参数分页查询操作")
    @ApiImplicitParam(name = "equipmentModelQueryPageParamVO", value = "设备模型参数分页查询VO对象", required = true, paramType = "query", dataType = "EquipmentModelQueryPageParamVO")
    @ApiOperationSupport(order = 1)
    @PostMapping("/queryPageParam")
    @ResponseBody
    public WebResponse<?> queryPageParam(@RequestBody EquipmentModelQueryPageParamVO equipmentModelQueryPageParamVO) {
        PageData<EquipmentModelParamVO> equipmentModelParamVOPageData = equipmentModelService.queryPageParam(equipmentModelQueryPageParamVO);
        return WebResponse.success(equipmentModelParamVOPageData);
    }

    /**
     * 设备模型分页查询操作
     *
     * @param equipmentModelPageQueryParamVO
     * @return
     */
    @ApiOperation("设备模型分页查询操作")
    @ApiImplicitParam(name = "equipmentModelPageQueryParamVO", value = "设备模型分页查询VO对象", required = true, paramType = "query", dataType = "EquipmentModelPageQueryParamVO")
    @ApiOperationSupport(order = 2)
    @PostMapping("/queryPage")
    @ResponseBody
    public WebResponse<?> queryPage(@RequestBody EquipmentModelPageQueryParamVO equipmentModelPageQueryParamVO) {
        PageData<EquipmentModelVO> equipmentModelVOPageData = equipmentModelService.queryPage(equipmentModelPageQueryParamVO);
        return WebResponse.success(equipmentModelVOPageData);
    }


    /**
     * 设备模型信息修改操作
     *
     * @param equipmentModelVO
     * @return
     */
    @ApiOperation("设备模型信息修改操作")
    @ApiImplicitParam(name = "equipmentModelVO", value = "设备模型修改VO对象", required = true, paramType = "query", dataType = "EquipmentModelVO")
    @ApiOperationSupport(order = 3)
    @PostMapping("/update")
    @ResponseBody
    public WebResponse<?> update(@RequestBody EquipmentModelVO equipmentModelVO) {
        EquipmentModelVO equipmentModelVOFirst = equipmentModelService.update(equipmentModelVO);
        return WebResponse.success(equipmentModelVOFirst);
    }

    /**
     * 设备模型参数信息添加操作
     *
     * @param equipmentModelParamVO
     * @return
     */
    @ApiOperation("设备模型参数信息添加操作")
    @ApiImplicitParam(name = "equipmentModelParamVO", value = "设备模型参数添加VO对象", required = true, paramType = "query", dataType = "EquipmentModelParamVO")
    @ApiOperationSupport(order = 4)
    @PostMapping("/savaParam")
    @ResponseBody
    public WebResponse<?> savaParam(@RequestBody EquipmentModelParamVO equipmentModelParamVO) {
        equipmentModelService.savaParam(equipmentModelParamVO);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }

    /**
     * 设备模型参数信息删除操作
     *
     * @param id
     * @return
     */
    @ApiOperation("设备模型参数信息删除操作")
    @ApiImplicitParam(name = "id", value = "设备模型参数id", required = true, paramType = "path")
    @ApiOperationSupport(order = 5)
    @GetMapping("/deleteModelParam/{id}")
    @ResponseBody
    public WebResponse<?> deleteModelParam(@PathVariable("id") Long id) {
        equipmentModelService.deleteModelParam(id);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }

    /**
     * 设备模型信息删除操作
     *
     * @param id
     * @return
     */
    @ApiOperation("设备模型信息删除操作")
    @ApiImplicitParam(name = "id", value = "设备模型id", required = true, paramType = "path")
    @ApiOperationSupport(order = 6)
    @GetMapping("/deleteModel/{id}")
    @ResponseBody
    public WebResponse<?> deleteModel(@PathVariable("id") Long id) {
        equipmentModelService.deleteModel(id);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }

    /**
     * 设备模型信息添加操作
     *
     * @param equipmentModelVO
     * @return
     */
    @ApiOperation("设备模型信息添加操作")
    @ApiImplicitParam(name = "equipmentModelParamVO", value = "设备模型参数添加VO对象", required = true, paramType = "query", dataType = "EquipmentModelParamVO")
    @ApiOperationSupport(order = 7)
    @PostMapping("/save")
    @ResponseBody
    public WebResponse<?> save(@RequestBody EquipmentModelVO equipmentModelVO) {
        EquipmentModelVO equipmentModelVOFrist = equipmentModelService.save(equipmentModelVO);
        return WebResponse.success(equipmentModelVOFrist);
    }

    /**
     * 通过设备模型id查询单个设备模型信息
     *
     */
    @ApiOperation("设备模型信息详情操作")
    @ApiImplicitParam(name = "id", value = "设备模型id", required = true, paramType = "path")
    @ApiOperationSupport(order = 8)
    @GetMapping("/detailsModel/{id}")
    @ResponseBody
    public WebResponse<?> detailsModel(@PathVariable("id") Long id) {
        EquipmentModelVO equipmentModelVO = equipmentModelService.detailsModel(id);
        return WebResponse.success(equipmentModelVO);
    }


    //
    @PostMapping("/export-template/{type}")
    @ApiOperation(value = "获取导入模板")
    public void exportTemplate(@PathVariable("type") String type, HttpServletResponse response) {
        EasyExcelUtils.exportExcelManySheet(response, "设备模型导入模版", ExcelSheetUtil.buildModelSheetList());
    }

    //
    @PostMapping("/export/{type}")
    @ApiOperation(value = "导出设备模型")
    public void export(@PathVariable("type") String type,
                       HttpServletResponse response,
                       @RequestBody EquipmentModelPageQueryParamVO equipmentModelPageQueryParamVO) {
        Collection<EquipmentModelVO> equipmentModelVOList = equipmentModelService.exportList(equipmentModelPageQueryParamVO);
        EasyExcelUtils.exportExcel(response,
                "设备模型",
                BeanUtils.copyToList(equipmentModelVOList, ExportExcelModelVO.class),
                ExportExcelModelVO.class);
    }

    @PostMapping("/import/{type}")
    @ApiOperation(value = "导入设备模型")
    public WebResponse<?> importExcel(@PathVariable("type") String type, @RequestPart @RequestParam("file") MultipartFile file) {
        List<ExportExcelModelVO> objects =
                (List<ExportExcelModelVO>) EasyExcelUtils.importExcel(file, ExportExcelModelVO.class);
        try{
            equipmentModelService.exportExcel(objects);
        }catch (ServiceException e){
            return WebResponse.error(e);
        }

        return WebResponse.success("操作成功");
    }
}
